package com.qingmeng.applyjob.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.qingmeng.applyjob.mapper.AdvertiseMapper;
import com.qingmeng.applyjob.mapper.UserAdvertiseRelationMapper;
import com.qingmeng.applyjob.model.Advertise;
import com.qingmeng.applyjob.model.AdvertiseExample;
import com.qingmeng.applyjob.model.UserAdvertiseRelation;
import com.qingmeng.applyjob.portal.model.dto.AdvertisePublishParam;
import com.qingmeng.applyjob.portal.model.vo.AdvertiseListVo;
import com.qingmeng.applyjob.portal.model.vo.AdvertiseVo;
import com.qingmeng.applyjob.portal.service.AdvertiseService;
import com.qingmeng.applyjob.portal.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AdvertiseServiceImpl implements AdvertiseService {

    private final static Logger logger = LoggerFactory.getLogger(AdvertiseServiceImpl.class);

    @Autowired
    private AdvertiseMapper advertiseMapper;
    @Autowired
    private UserAdvertiseRelationMapper userAdvertiseRelationMapper;


    /**
     * 发布招聘的实现
     * @param advertisePublishParam 要发布项目的信息（前端传来）
     * @return
     */
    @Transactional
    @Override
    public int publishTask(AdvertisePublishParam advertisePublishParam) {
        int result = 1;

        Advertise advertise = new Advertise();
        advertise.setWorkName(advertisePublishParam.getAdvertiseName());
        advertise.setLocation(advertisePublishParam.getWorkLocation());
        advertise.setDuration(advertisePublishParam.getWorkDuration());
        advertise.setEducation(advertisePublishParam.getEducationRequire());
        advertise.setMinSalary(advertisePublishParam.getMinSalary());
        advertise.setMaxSalary(advertisePublishParam.getMaxSalary());
        advertise.setUrl(advertisePublishParam.getUrl());
        advertise.setAdvantage(advertisePublishParam.getWorkAdvantage());
        advertise.setWorkDescribe(advertisePublishParam.getWorkDescribe());
        advertise.setSendRequire(advertisePublishParam.getSendRequire());
        advertise.setWorkType(advertisePublishParam.getWorkType());
        advertise.setCompanyName(advertisePublishParam.getCompanyName());
        advertise.setCompanyLogo(advertisePublishParam.getCompanyLogo());
        advertise.setCompanyProfile(advertisePublishParam.getCompanyProfile());
        advertise.setCreateTime(new Date());

        logger.info("此时招聘对象为{}",advertise);

        if(advertiseMapper.insertSelective(advertise) <= 0)
        {
            return 0;
        }else {
            UserAdvertiseRelation userAdvertiseRelation = new UserAdvertiseRelation();
            userAdvertiseRelation.setUserId(SecurityUtils.getCurrentUser().getId());
            userAdvertiseRelation.setAdvertiseId(advertise.getId());
            if (userAdvertiseRelationMapper.insertSelective(userAdvertiseRelation) <= 0){
                return 0;
            }
        }

        return result;
    }

    @Transactional
    @Override
    public List<AdvertiseListVo> advertiseList(int pageNum, int pageSize , int workType) {
        AdvertiseExample advertiseExample = new AdvertiseExample();
        advertiseExample.createCriteria().andWorkTypeEqualTo(workType);
        //分页插件只会对第一个sql语句起作用
        PageHelper.startPage(pageNum,pageSize);
        List<Advertise> advertises = advertiseMapper.selectByExample(advertiseExample);
        //承装 数据库的 数据
        List<AdvertiseListVo> advertiseListVos = new ArrayList<>();
        for (Advertise advertise : advertises){
            AdvertiseListVo advertiseListVo = new AdvertiseListVo();
            Long advertiseId = advertise.getId();

            //将数据库查询数据放入承载对象中
            advertiseListVo.setAdvertiseId(advertiseId);
            advertiseListVo.setAdvertiseName(advertise.getWorkName());
            advertiseListVo.setWorkLocation(advertise.getLocation());
            if (advertise.getCreateTime() == null){
                advertiseListVo.setCreateTime(null);
            }else {
                //修改时间戳
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM HH:mm");
                String format = simpleDateFormat.format(advertise.getCreateTime());
                advertiseListVo.setCreateTime(format);
            }
            advertiseListVo.setCompanyName(advertise.getCompanyName());
            advertiseListVo.setCompanyLogo(advertise.getCompanyLogo());
            advertiseListVo.setCompanyProfile(advertise.getCompanyProfile());
            //按照日薪转换年薪  将BigDecimal转换为int型
            int minSalary1 =  advertise.getMinSalary().intValue();
            int min = minSalary1 * 30;
            Integer mi = min/1000;
            String minS = mi.toString();
            int maxSalary1 = advertise.getMaxSalary().intValue();
            int max = maxSalary1 * 30;
            Integer ma = max/1000;
            String maxS = ma.toString();
            String minSalary = minS + "k";
            String maxSalary = maxS + "k";
            advertiseListVo.setMinSalary(minSalary);
            advertiseListVo.setMaxSalary(maxSalary);
            advertiseListVos.add(advertiseListVo);
        }
        return advertiseListVos;
    }


    @Transactional
    @Override
    public AdvertiseVo getAdvertise(Long advertiseId){
            AdvertiseExample advertiseExample = new AdvertiseExample();
            advertiseExample.createCriteria().andIdEqualTo(advertiseId);
        List<Advertise> advertises = advertiseMapper.selectByExample(advertiseExample);
        if (advertises != null && advertises.size() > 0){
            Advertise advertise = advertises.get(0);
            AdvertiseVo advertiseListVo = new AdvertiseVo();
            //将数据库查询数据放入承载对象中
            advertiseListVo.setAdvertiseId(advertiseId);
            advertiseListVo.setAdvertiseName(advertise.getWorkName());
            advertiseListVo.setWorkLocation(advertise.getLocation());
            advertiseListVo.setWorkDuration(advertise.getDuration());
            advertiseListVo.setEducationRequire(advertise.getEducation());
            advertiseListVo.setMaxSalary(advertise.getMaxSalary());
            advertiseListVo.setMinSalary(advertise.getMinSalary());
            advertiseListVo.setUrl(advertise.getUrl());
            advertiseListVo.setWorkAdvantage(advertise.getAdvantage());
            advertiseListVo.setWorkDescribe(advertise.getWorkDescribe());
            advertiseListVo.setSendRequire(advertise.getSendRequire());
            advertiseListVo.setWorkType(advertise.getWorkType());
            if (advertise.getCreateTime() == null){
                advertiseListVo.setCreateTime(null);
            }else {
                //修改时间戳
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = simpleDateFormat.format(advertise.getCreateTime());
                advertiseListVo.setCreateTime(format);
            }
            advertiseListVo.setCompanyName(advertise.getCompanyName());
            advertiseListVo.setCompanyLogo(advertise.getCompanyLogo());
            advertiseListVo.setCompamyProfile(advertise.getCompanyProfile());
            return advertiseListVo;
        }
        return null;
        }
}
